package com.happy.xiao.consumer.rabbitmq;


import com.happy.xiao.common.rabbitmq.BaseConsumerListener;
import com.happy.xiao.common.rabbitmq.util.MessageUtil;
import com.happy.xiao.common.utils.StringUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 死信队列的-转发队列监听器
 */
@Component("DLXMessageListener")
public class DLXMessageListener extends BaseConsumerListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String redisPrefix = "rabbitmq-delay-";

        try {
            String messageContent = MessageUtil.getMessageContent(message);
            System.out.println(messageContent);
            handleSuccessMessage(channel, message, redisPrefix);

        } catch (Exception e) {
            e.printStackTrace();

            // TODO: 这里要用缓存控制一下，重入队列
            // 先判断redis中，是否存在着个异常消息次数，如果不存在，则添加到redis
            // 判断redis中的次数，如果超过一定的次数，如10次，则把这个消息丢弃
            handleErrorMessage(channel, message, redisPrefix, 7);
        }
    }
}
